package com.tianji.aigc.controller;

import com.tianji.common.annotations.NoWrapper;
import com.tianji.aigc.dto.ChatDTO;
import com.tianji.aigc.service.ChatService;
import com.tianji.aigc.vo.ChatEventVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 *
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat")
public class ChatController {
    private final ChatService chatService;;

    @NoWrapper
    @PostMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatEventVO> chat(@RequestBody ChatDTO chatDTO) {
        log.info("chat方法");
        return this.chatService.chat(chatDTO.getQuestion(), chatDTO.getSessionId());
    }

    @PostMapping
    public void stop(@RequestParam("sessionId") String sessionId) {
        chatService.stop(sessionId);
    }

    @PostMapping("/text")
    public String chatText(@RequestBody String question) {
        return this.chatService.chatText(question);
    }

    /**
     * 处理多模型流式聊天请求的端点方法
     * 该方法接收用户问题、会话ID和可选文件列表，返回流式文本响应
     *
     * @param question 用户输入的问题内容
     * @param sessionId 唯一会话标识符，用于关联聊天上下文
     * @param files 可选的上传文件列表，用于多模态处理（可为空）
     * @return Flux<String> 流式返回的文本响应，按事件流格式传输
     */
    @PostMapping(value = "stream-multi-model", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chatStreamMultiModel(@RequestParam("question") String question,
                                             @RequestParam("sessionId") String sessionId,
                                             @RequestParam(value = "files", required = false) List<MultipartFile> files) {
        return chatService.chatStreamMultiModel(question, sessionId, files);
    }

}
